/**
 * Author: Jordi Vilaplana Mayoral <jvilaplana@alumnes.udl.cat>
 * Date: 05/04/2011
 * 
 * LocationManager
 * Copyright (C) 2011 Jordi Vilaplana Mayoral
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 * 
 * @(#)
 * Description: This program allows users to book rooms and schedule activities
 */

package locmanager.servlets;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import locmanager.beans.EquipmentBean;
import locmanager.beans.LocationBean;
import locmanager.persistence.DerbyPersistence;

/**
 * Modify Location Controller.
 * 
 * @author Jordi Vilaplana Mayoral <jvilaplana@alumnes.udl.cat>
 * @version 1.0
 */
public class ModifyLocationServlet extends HttpServlet {

  /**
   * Shows the modify location page.
   * 
   * @param request
   * @param response
   * @throws ServletException
   * @throws IOException 
   */
  private void showModifyLocationMenu(HttpServletRequest request, HttpServletResponse response)
          throws ServletException, IOException {

    request.setAttribute("m", "modifyLocation");

    getServletConfig().getServletContext().
            getRequestDispatcher("/index.jsp").forward(request, response);

  }

  /** 
   * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
   * @param request servlet request
   * @param response servlet response
   * @throws ServletException if a servlet-specific error occurs
   * @throws IOException if an I/O error occurs
   */
  protected void processRequest(HttpServletRequest request, HttpServletResponse response)
          throws ServletException, IOException {

    if (request.getParameter("locationId") != null) {

      int locationId = Integer.parseInt(request.getParameter("locationId"));
      String publicName = (String) request.getParameter("publicName");
      int roomTypeId = Integer.parseInt(request.getParameter("roomTypeId"));
      int capacity = Integer.parseInt(request.getParameter("capacity"));
      String[] equipmentList = request.getParameterValues("equipmentList");

      List<EquipmentBean> equipments = new ArrayList<EquipmentBean>();
      for (String equipment : equipmentList) {
        equipments.add(new EquipmentBean(Integer.parseInt(equipment)));
      }

      DerbyPersistence db = new DerbyPersistence();
      if (db.updateLocation(new LocationBean(locationId, publicName, roomTypeId, capacity, equipments))) {
        request.setAttribute("success", true);
      } else {
        request.setAttribute("success", false);
      }

      request.setAttribute("publicName", publicName);

    }

    showModifyLocationMenu(request, response);

  }

  /** 
   * Handles the HTTP <code>GET</code> method.
   * @param request servlet request
   * @param response servlet response
   * @throws ServletException if a servlet-specific error occurs
   * @throws IOException if an I/O error occurs
   */
  @Override
  protected void doGet(HttpServletRequest request, HttpServletResponse response)
          throws ServletException, IOException {
    processRequest(request, response);
  }

  /** 
   * Handles the HTTP <code>POST</code> method.
   * @param request servlet request
   * @param response servlet response
   * @throws ServletException if a servlet-specific error occurs
   * @throws IOException if an I/O error occurs
   */
  @Override
  protected void doPost(HttpServletRequest request, HttpServletResponse response)
          throws ServletException, IOException {
    processRequest(request, response);
  }

}
